/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package com.click.app.model;


import java.io.Serializable;
import java.util.Iterator;
import java.util.List;
import javax.persistence.Basic;
import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.NamedQueries;
import javax.persistence.NamedQuery;
import javax.persistence.OneToMany;
import javax.persistence.Table;
import javax.persistence.Transient;
import org.apache.commons.lang.builder.ToStringBuilder;
import org.apache.commons.lang.builder.ToStringStyle;

/**
 *
 * @author ronald
 */

@Entity
@Table(name = "pant_details")
@NamedQueries({@NamedQuery(name = "PantDetail.findAll", query = "SELECT pd FROM PantDetail pd")})
public class PantDetail extends BaseObject implements Serializable {

 
    private static final long serialVersionUID = 1L;

    private Long id;
    private Integer quantity;
    private Float priceXclothes;
    private Float totalAmountClothes;

    private Integer quantityFail;
    private Float priceXclotheFail;
    private Float totalAmountFailClothes;

    private Float totalAmount;
    private Float currentAmount;

    private Integer quantityRealClothes;
    private Integer quantityRealClothesCurrent; // quantity gets current
    
    private List<PantDetailCutDetail> pantDetailCutDetails;
    private ProcessDressmaker processDressmaker;
    private StorageCut storageCut;
    private Long idTemp;

    private boolean onStock;

    private List<PantDetailTradeMark> pantDetailTradeMarks;
    private String distribution;

    public PantDetail(){ }


    @Column(name = "current_amount")
    public Float getCurrentAmount() {
        return currentAmount;
    }

    public void setCurrentAmount(Float currentAmount) {
        this.currentAmount = currentAmount;
    }

    @Id
    @GeneratedValue(strategy = GenerationType.SEQUENCE)
    @Basic(optional = false)
    @Column(unique=true, name = "id_pant_detail")
    public Long getId() {
        return id;
    }

    public void setId(Long id) {
        this.id = id;
    }
    @Column(name = "price_x_clothes")
    public Float getPriceXclothes() {
        return priceXclothes;
    }
    
    public void setPriceXclothes(Float priceXclothes) {
        this.priceXclothes = priceXclothes;
    }
    @Column(name = "price_x_clothe_fail")
    public Float getPriceXclotheFail() {
        return priceXclotheFail;
    }

    public void setPriceXclotheFail(Float priceXclotheFail) {
        this.priceXclotheFail = priceXclotheFail;
    }
    
    @ManyToOne(fetch=FetchType.LAZY)
    @JoinColumn(name="id_storage_cut")
    public StorageCut getStorageCut() {
        return storageCut;
    }

    public void setStorageCut(StorageCut storageCut) {
        this.storageCut = storageCut;
    }


    @Column(name = "quantity")
    public Integer getQuantity() {
        return quantity;
    }

    public void setQuantity(Integer quantity) {
        this.quantity = quantity;
    }

    @Column(name = "quantity_fail")
    public Integer getQuantityFail() {
        return quantityFail;
    }

    public void setQuantityFail(Integer quantityFail) {
        this.quantityFail = quantityFail;
    }

    @Column(name = "total_amount")
    public Float getTotalAmount() {
        return totalAmount;
    }

    public void setTotalAmount(Float totalAmount) {
        this.totalAmount = totalAmount;
    }
    
    @OneToMany(cascade = CascadeType.ALL, fetch = FetchType.LAZY, mappedBy = "pantDetail")
    public List<PantDetailCutDetail> getPantDetailCutDetails() {
        return pantDetailCutDetails;
    }

    public void setPantDetailCutDetails(List<PantDetailCutDetail> pantDetailCutDetails) {
        this.pantDetailCutDetails = pantDetailCutDetails;
    }

    @ManyToOne(fetch=FetchType.LAZY)
    @JoinColumn(name="id_process_dressmaker",  nullable = false)
    public ProcessDressmaker getProcessDressmaker() {
        return processDressmaker;
    }

    public void setProcessDressmaker(ProcessDressmaker processDressmaker) {
        this.processDressmaker = processDressmaker;
    }

     @Column(name = "quantity_real_clothes")
    public Integer getQuantityRealClothes() {
        return quantityRealClothes;
    }

    public void setQuantityRealClothes(Integer quantityRealClothes) {
        this.quantityRealClothes = quantityRealClothes;
    }

    @Column(name = "quantity_real_clothes_current")
    public Integer getQuantityRealClothesCurrent() {
        return quantityRealClothesCurrent;
    }

    public void setQuantityRealClothesCurrent(Integer quantityRealClothesCurrent) {
        this.quantityRealClothesCurrent = quantityRealClothesCurrent;
    }

    @Column(name = "total_amount_clothes")
    public Float getTotalAmountClothes() {
        return totalAmountClothes;
    }

    public void setTotalAmountClothes(Float totalAmountClothes) {
        this.totalAmountClothes = totalAmountClothes;
    }

    @Column(name = "total_amount_fail_clothes")
    public Float getTotalAmountFailClothes() {
        return totalAmountFailClothes;
    }

    public void setTotalAmountFailClothes(Float totalAmountFailClothes) {
        this.totalAmountFailClothes = totalAmountFailClothes;
    }

    @OneToMany(cascade = CascadeType.ALL, fetch = FetchType.LAZY, mappedBy = "pantDetail")
    public List<PantDetailTradeMark> getPantDetailTradeMarks() {
        return pantDetailTradeMarks;
    }

    public void setPantDetailTradeMarks(List<PantDetailTradeMark> pantDetailTradeMarks) {
        this.pantDetailTradeMarks = pantDetailTradeMarks;
    }

    @Transient
    public String getIdentifier() {
        return storageCut.getPantDesign().getModel().getDatas() + ", " + storageCut.getPantDesign().getModelSize().getValueSize() + ", " + processDressmaker.getEndDate();
    }

    @Override
    public String toString() {
        return new ToStringBuilder(this, ToStringStyle.SIMPLE_STYLE)
                .append(this.id).append(this.quantity)
                .append(this.priceXclothes).append(this.quantityFail).append(this.priceXclotheFail)
                .append(this.totalAmount).append(this.currentAmount).toString();
    }

    @Override
    public boolean equals(Object obj) {
        if (obj == null) {
            return false;
        }
        if (getClass() != obj.getClass()) {
            return false;
        }
        final PantDetail other = (PantDetail) obj;
        if (this.id != other.id && (this.id == null || !this.id.equals(other.id))) {
            return false;
        }
        if (this.quantity != other.quantity && (this.quantity == null || !this.quantity.equals(other.quantity))) {
            return false;
        }
        if (this.priceXclothes != other.priceXclothes && (this.priceXclothes == null || !this.priceXclothes.equals(other.priceXclothes))) {
            return false;
        }
        if (this.totalAmountClothes != other.totalAmountClothes && (this.totalAmountClothes == null || !this.totalAmountClothes.equals(other.totalAmountClothes))) {
            return false;
        }
        if (this.quantityFail != other.quantityFail && (this.quantityFail == null || !this.quantityFail.equals(other.quantityFail))) {
            return false;
        }
        if (this.priceXclotheFail != other.priceXclotheFail && (this.priceXclotheFail == null || !this.priceXclotheFail.equals(other.priceXclotheFail))) {
            return false;
        }
        if (this.totalAmountFailClothes != other.totalAmountFailClothes && (this.totalAmountFailClothes == null || !this.totalAmountFailClothes.equals(other.totalAmountFailClothes))) {
            return false;
        }
        if (this.totalAmount != other.totalAmount && (this.totalAmount == null || !this.totalAmount.equals(other.totalAmount))) {
            return false;
        }
        if (this.currentAmount != other.currentAmount && (this.currentAmount == null || !this.currentAmount.equals(other.currentAmount))) {
            return false;
        }
        if (this.quantityRealClothes != other.quantityRealClothes && (this.quantityRealClothes == null || !this.quantityRealClothes.equals(other.quantityRealClothes))) {
            return false;
        }
        if (this.quantityRealClothesCurrent != other.quantityRealClothesCurrent && (this.quantityRealClothesCurrent == null || !this.quantityRealClothesCurrent.equals(other.quantityRealClothesCurrent))) {
            return false;
        }
        if (this.pantDetailCutDetails != other.pantDetailCutDetails && (this.pantDetailCutDetails == null || !this.pantDetailCutDetails.equals(other.pantDetailCutDetails))) {
            return false;
        }
        if (this.processDressmaker != other.processDressmaker && (this.processDressmaker == null || !this.processDressmaker.equals(other.processDressmaker))) {
            return false;
        }
        return true;
    }

    @Override
    public int hashCode() {
        int hash = 3;
        hash = 13 * hash + (this.id != null ? this.id.hashCode() : 0);
        hash = 13 * hash + (this.quantity != null ? this.quantity.hashCode() : 0);
        hash = 13 * hash + (this.priceXclothes != null ? this.priceXclothes.hashCode() : 0);
        hash = 13 * hash + (this.totalAmountClothes != null ? this.totalAmountClothes.hashCode() : 0);
        hash = 13 * hash + (this.quantityFail != null ? this.quantityFail.hashCode() : 0);
        hash = 13 * hash + (this.priceXclotheFail != null ? this.priceXclotheFail.hashCode() : 0);
        hash = 13 * hash + (this.totalAmountFailClothes != null ? this.totalAmountFailClothes.hashCode() : 0);
        hash = 13 * hash + (this.totalAmount != null ? this.totalAmount.hashCode() : 0);
        hash = 13 * hash + (this.currentAmount != null ? this.currentAmount.hashCode() : 0);
        hash = 13 * hash + (this.quantityRealClothes != null ? this.quantityRealClothes.hashCode() : 0);
        hash = 13 * hash + (this.quantityRealClothesCurrent != null ? this.quantityRealClothesCurrent.hashCode() : 0);
        hash = 13 * hash + (this.pantDetailCutDetails != null ? this.pantDetailCutDetails.hashCode() : 0);
        hash = 13 * hash + (this.processDressmaker != null ? this.processDressmaker.hashCode() : 0);
        return hash;
    }
    
    @Transient
    public Long getIdTemp() {
        return idTemp;
    }

    public void setIdTemp(Long idTemp) {
        this.idTemp = idTemp;
    }

    @Column(name = "on_stock")
    public boolean isOnStock() {
        return onStock;
    }

    public void setOnStock(boolean onStock) {
        this.onStock = onStock;
    }

    @Transient
    public Integer getCurrentQuantity(){
        Integer res = 0;
        Integer sumPdtm = 0;
        for (Iterator<PantDetailTradeMark> it = pantDetailTradeMarks.iterator(); it.hasNext();) {
            PantDetailTradeMark pdtm = it.next();
            sumPdtm+= pdtm.getQuantity();
        }
        res = quantityRealClothes - sumPdtm;
        return res;
    }
    @Column(name = "distribution")
    public String getDistribution() {
        return distribution;
    }
    
    public void setDistribution(String distribution) {
        this.distribution = distribution;
    }
    
    @Transient
    public void updateDistribution() {
        StringBuffer res = new StringBuffer("");
        for (Iterator<PantDetailTradeMark> it = pantDetailTradeMarks.iterator(); it.hasNext();) {
            PantDetailTradeMark pantDetailTradeMark = it.next();
            res.append(" ["+pantDetailTradeMark.getTradeMark().getName()+"->"+pantDetailTradeMark.getQuantity()+"] ");
        }
        
        setDistribution(res.toString());
    }

}
